<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>userManager</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="../commons/bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <!-- 加载bootstrap-select -->
    <link href="../commons/bootstrap-select-1.13.9-dist/css/bootstrap-select.min.css" rel="stylesheet">
    <!-- 加载bootstrap-treeview -->
    <link href="../commons/bootstrap-treeview-1.2.0-dist/bootstrap-treeview.min.css" rel="stylesheet">
    <link rel="stylesheet" href="../commons/css/currency.css">


    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#example-navbar-collapse">
                <span class="sr-only">切换导航</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">CIMS-校园即时通讯系统</a>
        </div>
        <div class="collapse navbar-collapse" id="example-navbar-collapse">
            <ul class="nav navbar-nav navbar-left">
                <li class="active"><a href="unitIndex"> 首页</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false"> 单位管理<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="unitTypeManager"> 单位类型</a></li>
                        <li><a href="unitManager"> 单位设置</a></li>
                    </ul>
                </li>
                <li><a href="userManager"> 用户管理</a></li>
            </ul>
            <ul class="nav navbar-nav" style="float: right">
                <li class=""><a id="loginUser_nav">管理员：</a></li>
                <li class=""><a id="logout_nav">注销</a></li>
            </ul>
        </div>
    </div>
    </div>
</nav>

<div class="container">
    <div class="page-header">
        <h3 style="align-content: center">院校管理员管理</h3>
    </div>

    <div class="row" style="margin: 0 0 0;">
        <div class="col-md-4">
            <div class="col-md-3" style="padding: 7px 0 7px;">
                <p>单位类型：</p>
            </div>
            <div class="col-md-9">
                <select class="selectpicker form-control" title="请选择单位类型" id="unitType_search_select">

                </select>
            </div>
        </div>
        <div class="col-md-4">
            <div class="col-md-3" style="padding: 7px 0 7px;">
                <p>单位：</p>
            </div>
            <div class="col-md-9">
                <select class="selectpicker form-control" title="请选择单位" id="unit_search_select" disabled>

                </select>
            </div>
        </div>
        <div class="col-md-4">
            <div class="col-md-12">
                <div class="input-group">
                    <input type="text" class="form-control" placeholder="请输入用户名" id="userName_search_input">
                    <span class="input-group-btn">
                        <button class="btn btn-primary" id="search_btn"><span class="glyphicon glyphicon-search"></span>
                            搜索</button>
                        <button class="btn btn-warning" id="reset_btn"><span class="glyphicon glyphicon-refresh"></span>
                            重置</button>
                    </span>
                </div>
            </div>
        </div>
    </div>
    <h5 class="page-header" style="margin: 0 10px 10px 10px;"></h5>
    <div class="col-md-12">
        <table class="table table-bordered table-striped table-hover" id="users-table">
            <thead>
            <tr>
                <th><input type="checkbox" id="check_all"></th>
                <th>用户名</th>
                <th>所属单位</th>
                <th>操作人</th>
                <th>操作时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>

            </tbody>
            <tfoot>
            </tfoot>
        </table>
    </div>
    </td>
    </tr>
    </tfoot>
    </table>
    <!-- 添加模态框（Modal） -->
    <div class="modal fade" id="user_add_modal" tabindex="-1" role="dialog" aria-labelledby="user_add_modal"
         aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title"> 新增用户</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form" id="user_add_frm">
                        <div class="form-group">
                            <label for="userName_add_modal" class="col-sm-2 control-label"> 用户名：</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="userName" id="userName_add_modal"
                                       placeholder="请输入用户名">
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="userPassword_add_modal" class="col-sm-2 control-label"> 密码：</label>
                            <div class="col-sm-10">
                                <input type="password" class="form-control" name="userPassword"
                                       id="userPassword_add_modal"
                                       placeholder="请输入密码">
                                <span class="help-block"></span>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal"> 关闭</button>
                    <button type="button" class="btn btn-primary" id="user_save_btn"> 保存</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
    <!-- 修改模态框（Modal） -->
    <div class="modal fade" id="user_modify_modal" tabindex="-1" role="dialog" aria-labelledby="user_modify_modal"
         aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title"> 修改用户</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form" id="user_modify_frm">
                        <div class="form-group">
                            <label for="userName_modify_modal" class="col-sm-2 control-label"> 用户名：</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="userName" id="userName_modify_modal"
                                       placeholder="请输入用户名">
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="userPassword_modify_modal" class="col-sm-2 control-label"> 密码：</label>
                            <div class="col-sm-10">
                                <input type="password" class="form-control" name="userPassword"
                                       id="userPassword_modify_modal"
                                       placeholder="请输入密码">
                                <span class="help-block"></span>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal"> 关闭</button>
                    <button type="button" class="btn btn-primary" id="user_modify_btn"> 保存</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
    <!-- 单位分配模态框（Modal） -->
    <div class="modal fade" id="unit_assign_modal" tabindex="-1" role="dialog" aria-labelledby="unit_assign_modal"
         aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title"> 单位分配</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form" id="unit_assign_frm">
                        <div class="form-group">
                            <label for="userName_assign_modal" class="col-sm-3 control-label"> 用户名：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="userName" id="userName_assign_modal"
                                       placeholder="请输入用户名" disabled>
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-sm-3 control-label"> 所属单位：</label>
                            <div class="col-sm-4" id="unit_odd_div">

                            </div>
                            <div class="col-sm-4" id="unit_even_div">
                                <div class="panel-group" id="accordion1">
                                    <div class="panel panel-default">
                                        <div class="panel-heading">
                                            <h4 class="panel-title">
                                                <input type="checkbox"/>
                                            </h4>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal"> 关闭</button>
                    <button type="button" class="btn btn-primary" id="unit_assign_btn"> 保存</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>


    <div class="footer ">
        <div class="container">
            <div class="row footer-bottom">
                <ul class="list-inline text-center">
                    <li> Campus Instant Messaging System @2020 杭州职业技术学院 软件技术专业</li>
                </ul>
            </div>
        </div>
    </div>
</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="../commons/js/jquery.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="../commons/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script src="../commons/bootstrap-treeview-1.2.0-dist/bootstrap-treeview.min.js"></script>
<script src="../commons/bootstrap-select-1.13.9-dist/js/bootstrap-select.min.js"></script>
<script src="../commons/js/login.js"></script>
<script type="text/javascript">
    var unitTypeId, unitId, userName, pn;

    $(function () {
        pn = 1;
        getUnitTypeList("#unitType_search_select");
        to_page(pn);
    });

    //实现获取用户所有信息
    function to_page(pn) {
        $.ajax({
            url: "../user/users",
            type: "GET",
            data: {"unitTypeId": unitTypeId, "unitId": unitId, "userName": userName, "pn": pn},
            dataType: "json",
            beforeSend: function () {
                $("#users-table tbody").empty();
                $("#users-table tfoot").empty();
                $("#users-table tbody").append("<tr><td colspan='6'>正在加载数据，请稍后...</td></tr>");
            },
            success: function (result) {
                if (result.code == 100) {
                    //查询成功，解析并显示数据
                    //1、显示用户信息
                    build_users_table(result);
                    //2、显示分页信息
                    build_page_nav(result);
                } else {

                }
            },
            error: function () {
                $("#users-table tbody").empty();
                $("#users-table tfoot").empty();
                $("#users-table tbody").append("<tr><td colspan='6'>查询出错，请稍后再试！</td></tr>");
            }
        });
    }

    function build_users_table(result) {
        $("#users-table tbody").empty();
        var users = result.extend.pageInfo.list;
        $.each(users, function (index, user) {
            var chkTd = $("<td></td>").addClass("tdCenter").append($("<input>").addClass("check_item").attr({"type": "checkbox"}));
            var userUserNameTd = $("<td></td>").append(user.userName);
            var userUserAreaTd = $("<td></td>");
            $.each(user.units, function (index, unit) {
                userUserAreaTd.append($("<p></p>").append(unit.unitName));
            });
            var userUserOperatorTd = $("<td></td>").addClass("tdCenter").append(user.userOperator.userName);
            var userUserOperatorTimeTd = $("<td></td>").addClass("tdCenter").append(user.userOperatorTime);
            var editBtn = $("<button></button>").addClass("btn btn-sm btn-primary edit_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-edit")).append(" 修改");
            editBtn.attr({"edit-id": user.id, "pn": result.extend.pageInfo.pageNum});
            var assignBtn = $("<button></button>").addClass("btn btn-sm btn-secondary assign_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-stats")).append(" 单位分配");
            assignBtn.attr({"assign-id": user.id, "pn": result.extend.pageInfo.pageNum});
            var delBtn = $("<button></button>").addClass("btn btn-sm btn-danger delete_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-remove")).append(" 删除");
            delBtn.attr("delete-id", user.id);
            var userOperationDiv = $("<div></div>").addClass("input-group").append($("<span></span>").addClass("input-group-btn").append(editBtn).append(assignBtn).append(delBtn));
            var userOperationTd = $("<td></td>").addClass("tdCenter").append(userOperationDiv);

            $("<tr></tr>").append(chkTd)
                .append(userUserNameTd)
                .append(userUserAreaTd)
                .append(userUserOperatorTd)
                .append(userUserOperatorTimeTd)
                .append(userOperationTd)
                .appendTo("#users-table tbody");
        });
    }

    function build_page_nav(result) {
        $("#users-table tfoot").empty();
        var operatorUl = $("<ul></ul>").addClass("pagination pull-left").attr("style", "margin: 0 0 0 0;");
        var addLi = $("<li></li>").append($("<a></a>").attr("id", "user_add_btn").append($("<span></span>").addClass("glyphicon glyphicon-plus").append("新增")));
        var removeLi = $("<li></li>").append($("<a></a>").attr("id", "user_delete_all_btn").append($("<span></span>").addClass("glyphicon glyphicon-remove").append("批量删除")));
        operatorUl.append(addLi).append(removeLi);

        var pageUl = $("<ul></ul>").addClass("pagination").attr("style", "margin: 0 0 0 0;");
        var firstPageLi = $("<li></li>").append($("<a></a>").append("首页"));
        ;
        if (result.extend.pageInfo.isFirstPage) {
            firstPageLi.addClass("disabled");
        } else {
            firstPageLi.click(function () {
                to_page(1);
            });
        }
        pageUl.append(firstPageLi);

        var previousPageLi = $("<li></li>").append($("<a></a>").append("《"));
        if (result.extend.pageInfo.hasPreviousPage) {
            previousPageLi.click(function () {
                to_page(result.extend.pageInfo.pageNum - 1);
            });
        } else {
            previousPageLi.addClass("disabled");
        }
        pageUl.append(previousPageLi);
        $.each(result.extend.pageInfo.navigatepageNums, function (index, npNum) {
            var pageLi = $("<li></li>").append($("<a></a>").append(npNum));
            if (result.extend.pageInfo.pageNum == npNum) {
                pageLi.addClass("active");
            } else {
                pageLi.click(function () {
                    to_page(npNum);
                });
            }
            pageUl.append(pageLi);
        });
        var nextPageLi = $("<li></li>").append($("<a></a>").append("》"));
        if (result.extend.pageInfo.hasNextPage) {
            nextPageLi.click(function () {
                to_page(result.extend.pageInfo.pageNum + 1);
            });
        } else {
            nextPageLi.addClass("disabled");
        }
        pageUl.append(nextPageLi);
        var lastPageLi = $("<li></li>").append($("<a></a>").append("末页"));
        if (result.extend.pageInfo.isLastPage) {
            lastPageLi.addClass("disabled");
        } else {
            lastPageLi.click(function () {
                to_page(result.extend.pageInfo.pages);
            });
        }
        pageUl.append(lastPageLi);

        var pageInfoUl = $("<ul></ul>").addClass("pagination pull-right").attr("style", "margin: 0 0 0 0;");
        var pageInfoLi = $("<li></li>").addClass("disabled").append($("<a></a>").attr("style", "border: 0px;cursor: default;").append("当前第 " + result.extend.pageInfo.pageNum + " 页，总 "
            + result.extend.pageInfo.pages + " 页，总 "
            + result.extend.pageInfo.total + " 条记录"));
        pageInfoUl.append(pageInfoLi);

        pn = result.extend.pageInfo.pageNum;

        var pageNavTd = $("<td></td>").addClass("text-center")
            .attr({"colspan": 6}).append(operatorUl).append(pageUl).append(pageInfoUl);

        $("<tr></tr>").append(pageNavTd)
            .appendTo("#users-table tfoot");
    }


    function getUnitTypeList(element) {
        $.ajax({
            url: "unitTypes",
            type: "GET",
            dataType: "json",
            success: function (result) {
                if (result.code == 100) {
                    $(element).empty();
                    optionElement = $("<option></option>").append("全部单位类型").attr({"value": 0});
                    optionElement.appendTo($(element));

                    $.each(result.extend.unitTypes, function (index, unitType) {
                        optionElement = $("<option></option>").append(unitType.unitTypeName).attr({"value": unitType.id});
                        optionElement.appendTo($(element));
                    });
                    $(element).selectpicker("refresh");
                }
            }
        });
    }

    $("#unitType_search_select").on("changed.bs.select", function () {

        $("#unit_search_select").empty();
        $("#unit_search_select").attr("disabled", "disabled");
        $("#unit_search_select").selectpicker("refresh");
        if ($(this).val() > 0) {
            getUnitList("#unit_search_select", $(this).val());
        }
    });

    function getUnitList(element, unitTypeId) {
        var url;
        $.ajax({
            url: "units",
            data: {"unitTypeId": unitTypeId, "pn": -1},
            type: "GET",
            dataType: "json",
            success: function (result) {
                $(element).empty();
                if (result.code == 100) {
                    $(element).empty();
                    optionElement = $("<option></option>").append("全部单位").attr({"value": 0});
                    optionElement.appendTo($(element));

                    $.each(result.extend.units, function (index, unit) {
                        optionElement = $("<option></option>").append(unit.unitName).attr({"value": unit.id});
                        optionElement.appendTo($(element));
                    });
                    $(element).removeAttr("disabled");
                    $(element).selectpicker("refresh");
                }
            }
        });
    }

    $("#search_btn").click(function () {
        if ($("#unitType_search_select").val() == "") {
            unitTypeId = 0;
        } else {
            unitTypeId = $("#unitType_search_select").val();
        }
        if ($("#unit_search_select").val() == "") {
            unitId = 0;
        } else {
            unitId = $("#unit_search_select").val();
        }
        userName = $("#userName_search_input").val();
        to_page(pn);
    });

    $("#reset_btn").click(function () {
        getUnitTypeList("#unitType_search_select");
        $("#unit_search_select").empty();
        $("#unit_search_select").attr("disabled", "disabled");
        $("#unit_search_select").selectpicker("refresh");
        $("#userName_search_input").val("");
    });


    $(document).on("click", ".delete_btn", function () {
        var userName = $(this).parents("tr").find("td:eq(1)").text();
        if (confirm("确认删除[" + userName + "]吗")) {
            $.ajax({
                url: "../user/user/" + $(this).attr("delete-id"),
                type: "delete",
                dataType: "json",
                success: function (result) {
                    alert(result.msg);
                    if (result.code = 100) {
                        //删除成功
                        //刷新用户列表显示
                        to_page(1);
                        $("#check_all").prop("checked", false);
                    }

                }
            })
        }
    })

    $("#check_all").click(function () {
        $(".check_item").prop("checked", $(this).prop("checked"));
    });

    $(document).on("click", ".check_item", function () {
        var flag = $(".check_item").length == $(".check_item:checked").length;
        $("#check_all").prop("checked", flag);
    });

    $(document).on("click", "#user_delete_all_btn", function () {
        var userNames = "", ids = "";
        $.each($(".check_item:checked"), function () {
            userNames = userNames + $(this).parents("tr").find("td:eq(1)").text() + ",";
            ids = ids + $(this).parents("tr").find("td:eq(5)").find(".delete_btn").attr("delete-id") + "-";
        });
        userNames = userNames.substring(0, userNames.length - 1);
        ids = ids.substring(0, ids.length - 1);
        if (confirm("确认删除[ " + userNames + " ]吗？")) {
            $.ajax({
                url: "../user/user/" + ids,
                type: "delete",
                dataType: "json",
                success: function (result) {
                    alert(result.msg);
                    if (result.code = 100) {
                        //删除成功
                        //刷新用户列表显示
                        to_page(1);
                        $("#check_all").prop("checked", false);
                    }
                }
            });
        }
    });

    $(document).on("click", "#user_add_btn", function () {
        $("#user_add_modal").modal({
            "backdrop": "static"
        });
    });

    $(document).on('hidden.bs.modal', '.modal', function () {
        $("#user_add_modal form")[0].reset();
        $("#user_add_modal form").find("*").removeClass("has-error has-success");
        $("#user_add_modal form").find(".help-block").text("");

        $("#user_modify_modal form")[0].reset();
        $("#user_modify_modal form").find("*").removeClass("has-error has-success");
        $("#user_modify_modal form").find(".help-block").text("");
    });

    //新增用户保存操作
    $("#user_save_btn").click(function () {
        //数据校验
        //新增院校管理员的异步请求
        $.ajax({
            url: "../user/user",
            data: $("#user_add_frm").serialize(),
            type: "post",
            dataType: "json",
            success: function (result) {
                if (result.code == 100) {
                    //添加成功，刷新所有用户信息
                    $("#user_add_modal").modal('hide');
                    alert("用户信息添加成功！");
                    to_page(1);
                    $("#check_all").prop("checked", false);
                    //自主完成
                }

            }
        })
    })


    //修改单位模态框保存操作，
    $(document).on("click", ".edit_btn", function () {
        $.ajax({
            url: "../user/user",
            data: {"id": $(this).attr("edit-id")},
            type: "GET",
            dataType: "json",
            success: function (result) {
                if (result.code == 100) {
                    $("#userName_modify_modal").val(result.extend.user.userName);
                    $("#userPassword_modify_modal").val(result.extend.user.userPassword);
                }
            }
        });

        $("#user_modify_btn").attr("edit-id", $(this).attr("edit-id"));
        $("#user_modify_modal").modal({
            "backdrop": "static"
        });
    });

    // 修改用户保存

    $("#user_modify_btn").click(function () {
        //数据校验
        //新增院校管理员的异步请求
        $.ajax({
            url: "../user/user/" + $(this).attr("edit-id"),
            data: $("#user_modify_frm").serialize(),
            type: "put",
            dataType: "json",
            success: function (result) {
                if (result.code == 100) {
                    //修改成功，刷新所有用户信息
                    $("#user_modify_modal").modal('hide');
                    alert("用户信息修改成功！");
                    to_page(1);
                    $("#check_all").prop("checked", false);
                    //自主完成
                }
            }
        });
    });

    //单位分配模态框
    $(document).on("click", ".assign_btn", function () {
        var assignId = $(this).attr("assign-id");
        $.ajax({
            url: "../unit/unitTypes",
            type: "GET",
            dataType: "json",
            success: function (result) {
                var maxIndex;
                if (result.code == 100) {
                    $("#unit_odd_div").empty();
                    $("#unit_even_div").empty();
                    $.each(result.extend.unitTypes, function (index, unitType) {
                        var remainder = index % 2;
                        var chkInput = $("<input></input>").attr({
                            "type": "checkbox",
                            "chkIndex": index
                        }).addClass("check_all check_all_" + index);
                        var titleA = $("<a></a>").attr({
                            "data-toggle": "collapse",
                            "data-parent": "#accordion" + index,
                            "href": "#collapse" + index
                        }).append(unitType.unitTypeName);
                        var h4 = $("<h4></h4>").addClass("panel-title").append(chkInput).append(titleA);
                        var panelHeadingDiv = $("<div></div>").addClass("panel-heading").append(h4);

                        var panelBodyDiv = $("<div></div>").addClass("panel-body");
                        $.each(unitType.units, function (i, unit) {
                            var checkInput = $("<input></input>").attr({
                                "name": "unitIds",
                                "type": "checkbox",
                                "chkIndex": index,
                                "value": unit.id
                            }).addClass("check_item check_item_" + index + " check_item_id_" + unit.id);
                            var lbl = $("<label></label>").append(unit.unitName);
                            panelBodyDiv.append(checkInput).append(lbl).append($("<br>"));
                        });
                        var bodyDiv = $("<div></div>").addClass("panel-collapse collapse").attr("id", "collapse" + index).append(panelBodyDiv);
                        var panelDefaultDiv = $("<div></div>").addClass("panel panel-default").append(panelHeadingDiv).append(bodyDiv);
                        var panelGroupDiv = $("<div></div>").addClass("panel-group").attr("id", "accordion" + index).append(panelDefaultDiv);
                        if (index % 2 == 0) {
                            $("#unit_odd_div").append(panelGroupDiv);
                        } else {
                            $("#unit_even_div").append(panelGroupDiv);
                        }
                        maxIndex = index;
                    });

                    $.ajax({
                        url: "../user/user",
                        data: {"id": assignId},
                        type: "GET",
                        dataType: "json",
                        success: function (result) {
                            if (result.code == 100) {
                                var flag;
                                $("#userName_assign_modal").val(result.extend.user.userName);

                                $.each(result.extend.user.units, function (i, unit) {
                                    $(".check_item_id_" + unit.id).prop("checked", true);
                                });
                                for (var i = 0; i <= maxIndex; i++) {
                                    flag = $(".check_item_" + i + ":checked").length == $(".check_item_" + i).length;
                                    $(".check_all_" + i).prop("checked", flag);
                                }
                                $("#unit_assign_btn").attr("assign-id", result.extend.user.id);
                            }
                        }
                    });
                }
            }
        });

        $("#unit_assign_modal").modal({
            "backdrop": "static"
        });
    });

    //单位分配保存
    $("#unit_assign_btn").click(function () {
        //数据校验

        //单位分配的异步请求
        $.ajax({
            url: "../user/userUnit/" + $(this).attr("assign-id"),
            data: $("#unit_assign_frm").serialize(),
            type: "put",
            dataType: "json",
            success: function (result) {
                if (result.code == 100) {
                    //修改成功，刷新所有用户信息
                    $("#unit_assign_modal").modal('hide');
                    alert("用户单位分配成功！");
                    to_page(1);
                    $("#check_all").prop("checked", false);
                    //自主完成
                }
            }
        });
    });

    $(document).on("click", ".check_all", function () {
        $(".check_item_" + $(this).attr("chkIndex")).prop("checked", $(this).prop("checked"));
    });

    $(document).on("click", ".check_item", function () {
        var flag = $(".check_item_" + $(this).attr("chkIndex") + ":checked").length == $(".check_item_" + $(this).attr("chkIndex")).length;
        $(".check_all_" + $(this).attr("chkIndex")).prop("checked", flag);
    });


</script>


</body>
</html>
